﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.EntityClass;

namespace Tunnel
{
    public partial class ManualEventForm : Form
    {
        ManualEventInfo mei = new ManualEventInfo();
        public ManualEventForm()
        {
            InitializeComponent();
        }

        private void add_Click(object sender, EventArgs e)
        {
            ManualEventInfoForm f = new ManualEventInfoForm();
            f.StartPosition = FormStartPosition.CenterScreen;
            DialogResult dr = f.ShowDialog();
            if (dr == DialogResult.OK)
            {
                FillTheGridView();
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除这条记录么？", "删除确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += DeleteMEI;
                bw.RunWorkerCompleted += DeleteMEIComplete;
                if (!(bw.IsBusy && string.IsNullOrEmpty(mei.MeID)))
                {
                    bw.RunWorkerAsync(mei.MeID);
                }
            }
        }

        private void DeleteMEIComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            int? result = e.Result as int?;
            if(result!=0)
            {
                MessageBox.Show("删除成功", "删除成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            FillTheGridView();
        }

        private void DeleteMEI(object sender, DoWorkEventArgs e)
        {
            string MEIID = e.Argument as string;
            string deleteSql = "delete from [H_ManualEventInfo] where MEIID='" + MEIID + "'";
            SqlDBHelper sdh = new SqlDBHelper();
            int result = sdh.ExecuteCommand(deleteSql);
            e.Result = result;
        }

        private void edit_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(mei.MeID))
            {
                MessageBox.Show("请选择您要编辑的记录", "未选择记录", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                ManualEventInfoForm f = new ManualEventInfoForm(mei);
                f.StartPosition = FormStartPosition.CenterScreen;

                DialogResult dr = f.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    FillTheGridView();
                }
            }
        }

        private void ManualEventForm_Load(object sender, EventArgs e)
        {
            FillTheGridView();

            dtpStartTime.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd ") + "00:00:00");
            dtpEndTime.Value = DateTime.Now;

            BackgroundWorker bwGetUser = new BackgroundWorker();
            bwGetUser.DoWork += BwGetUser_DoWork;
            bwGetUser.RunWorkerCompleted += BwGetUser_RunWorkerCompleted;

            if (!bwGetUser.IsBusy)
            {
                bwGetUser.RunWorkerAsync();
            }
            
        }

        private void BwGetUser_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            DataTable dtUser = e.Result as DataTable;
            

            cbOperator.DataSource = dtUser.Copy();
            cbModifier.DataSource = dtUser.Copy();

            cbOperator.Text = string.Empty;
            cbModifier.Text = string.Empty;
        }

        private void BwGetUser_DoWork(object sender, DoWorkEventArgs e)
        {
            string sql = "select * from M_User";
            SqlDBHelper sdh = new SqlDBHelper();
            DataTable dtUser = sdh.GetTable(sql);
            e.Result = dtUser;
        }

        private void FillTheGridView()
        {
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += FillGridBackground;
            bw.RunWorkerCompleted += FillGridComplete;
            if(!bw.IsBusy)
            {
                bw.RunWorkerAsync();
            }
        }

        private void FillGridComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            DataTable dtManualEventInfo = e.Result as DataTable;
            dgvManualEventInfo.DataSource = dtManualEventInfo;
        }

        private void FillGridBackground(object sender, DoWorkEventArgs e)
        {
            string sqlFillTheGridView = "SELECT H_ManualEventInfo.MEIID, H_ManualEventInfo.StartTime, H_ManualEventInfo.EndTime, H_ManualEventInfo.HappenPlace, H_ManualEventInfo.Type, H_ManualEventInfo.InfoSource, H_ManualEventInfo.Detail, H_ManualEventInfo.HandleMethod, H_ManualEventInfo.Operator, ModifierUserTable.UserID AS OperatorID, ModifierUserTable.Name AS OperatorName, H_ManualEventInfo.RegisterTime, H_ManualEventInfo.Modifier, OperatorUserTable.UserID AS ModifierID, OperatorUserTable.Name AS ModifierName, H_ManualEventInfo.LastModifyTime FROM H_ManualEventInfo LEFT OUTER JOIN M_User AS OperatorUserTable ON H_ManualEventInfo.Modifier = OperatorUserTable.UserID LEFT OUTER JOIN M_User AS ModifierUserTable ON H_ManualEventInfo.Operator = ModifierUserTable.UserID ORDER BY StartTime";
            SqlDBHelper sdh = new SqlDBHelper();
            DataTable dtManualEventInfo = sdh.GetTable(sqlFillTheGridView);
            e.Result = dtManualEventInfo;
        }

        private void dgvManualEventInfo_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvManualEventInfo.SelectedCells.Count > 0)
            {
                int rowIndex = this.dgvManualEventInfo.SelectedCells[0].RowIndex;
                if (dgvManualEventInfo.Rows[rowIndex].Cells["MEIID"].Value != null)
                {
                    mei.MeID = dgvManualEventInfo.Rows[rowIndex].Cells["MEIID"].Value.ToString();
                    if (dgvManualEventInfo.Rows[rowIndex].Cells["StartTime"].Value != null)
                    {
                        mei.StartTime = Convert.ToDateTime(dgvManualEventInfo.Rows[rowIndex].Cells["StartTime"].Value.ToString());
                    }
                    if (dgvManualEventInfo.Rows[rowIndex].Cells["EndTime"].Value != null)
                    {
                        mei.EndTime = Convert.ToDateTime(dgvManualEventInfo.Rows[rowIndex].Cells["EndTime"].Value.ToString());
                    }
                    mei.HappenPlace = dgvManualEventInfo.Rows[rowIndex].Cells["HappenPlace"].Value.ToString();
                    mei.Type = dgvManualEventInfo.Rows[rowIndex].Cells["Type"].Value.ToString();
                    mei.InfoSource = dgvManualEventInfo.Rows[rowIndex].Cells["InfoSource"].Value.ToString();
                    mei.Detail = dgvManualEventInfo.Rows[rowIndex].Cells["Detail"].Value.ToString();
                    mei.HandleMethod = dgvManualEventInfo.Rows[rowIndex].Cells["HandleMethod"].Value.ToString();
                    mei.OperatorUser = dgvManualEventInfo.Rows[rowIndex].Cells["OperatorID"].Value.ToString();
                    if (dgvManualEventInfo.Rows[rowIndex].Cells["RegisterTime"].Value != null)
                    {
                        mei.RegisterTime = Convert.ToDateTime(dgvManualEventInfo.Rows[rowIndex].Cells["RegisterTime"].Value.ToString());
                    }
                    mei.Modifier = dgvManualEventInfo.Rows[rowIndex].Cells["ModifierID"].Value.ToString();
                    if (dgvManualEventInfo.Rows[rowIndex].Cells["LastModifyTime"].Value != null && !string.IsNullOrEmpty(dgvManualEventInfo.Rows[rowIndex].Cells["LastModifyTime"].Value.ToString()))
                    {
                        mei.LastModifyTime = Convert.ToDateTime(dgvManualEventInfo.Rows[rowIndex].Cells["LastModifyTime"].Value.ToString());
                    }

                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            BackgroundWorker bwSearch = new BackgroundWorker();
            bwSearch.DoWork += BwSearch_DoWork;
            bwSearch.RunWorkerCompleted += BwSearch_RunWorkerCompleted;

            try
            {
                string sqlFillTheGridView = "SELECT H_ManualEventInfo.MEIID, H_ManualEventInfo.StartTime, H_ManualEventInfo.EndTime, H_ManualEventInfo.HappenPlace, H_ManualEventInfo.Type, H_ManualEventInfo.InfoSource, H_ManualEventInfo.Detail, H_ManualEventInfo.HandleMethod, H_ManualEventInfo.Operator, ModifierUserTable.UserID AS OperatorID, ModifierUserTable.Name AS OperatorName, H_ManualEventInfo.RegisterTime, H_ManualEventInfo.Modifier, OperatorUserTable.UserID AS ModifierID, OperatorUserTable.Name AS ModifierName, H_ManualEventInfo.LastModifyTime FROM H_ManualEventInfo LEFT OUTER JOIN M_User AS OperatorUserTable ON H_ManualEventInfo.Modifier = OperatorUserTable.UserID LEFT OUTER JOIN M_User AS ModifierUserTable ON H_ManualEventInfo.Operator = ModifierUserTable.UserID WHERE 1=1";
                if (!string.IsNullOrEmpty(cbOperator.Text))
                {
                    sqlFillTheGridView = sqlFillTheGridView + " and ModifierUserTable.UserID='" + cbOperator.SelectedValue + "' ";

                }
                if (!string.IsNullOrEmpty(cbModifier.Text))
                {
                    sqlFillTheGridView = sqlFillTheGridView + " and ModifierUserTable.UserID='" + cbModifier.SelectedValue + "' ";
                }
                sqlFillTheGridView = sqlFillTheGridView + " and H_ManualEventInfo.StartTime between '" + dtpStartTime.Text + "' and '" + dtpEndTime.Text + "' ORDER BY StartTime ";

                if (!bwSearch.IsBusy)
                {
                    bwSearch.RunWorkerAsync(sqlFillTheGridView);
                }
            }
            catch { }      
        }

        private void BwSearch_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            DataTable dtManualEventInfo = e.Result as DataTable;
            dgvManualEventInfo.DataSource = dtManualEventInfo;
        }

        private void BwSearch_DoWork(object sender, DoWorkEventArgs e)
        {
            string sql = e.Argument as string;
            SqlDBHelper sdh = new SqlDBHelper();
            DataTable dtManualEventInfo = sdh.GetTable(sql);
            e.Result = dtManualEventInfo;
        }
    }
}
